<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
        }
        .wrapper>div {
            margin: 20px 0;
        }
        .submit-btn {
            width: 80px;
            height: 30px;
            line-height: 30px;
            color: #fff;
            background-color: #3176bc;
            text-align: center;
        }
        footer {
            width: 1200px;
            background-color: pink;
        }
        footer .content {
            width: 100%;
            height: 40px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        footer .content .info {
            display: flex;
            height: 30px;
            line-height: 30px;
        }
        .text {
            margin-left: 20px;
        }
        .critic {
            background-color: skyblue;
            padding:0 20px;
            box-sizing: border-box;
            border-radius: 15px;
        }
        .time {
            background-color: #fecd84;
            margin-left: 20px;
            margin-right: 20px;
            padding:0 20px;
            box-sizing: border-box;
            border-radius: 15px;
        }
        textarea {
            resize: none;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <div class="text">评论人:</div>
        <input name="" id="" cols="30" rows="3" placeholder="请输入评论人"></input>
        <div class="text">评论内容:</div>
        <textarea name="" id="" cols="30" rows="5" placeholder="请输入评论内容 (最多120字)"></textarea>
        <div class="submit-btn">发表评论</div>
    </div>
    <footer>
        <!-- <div class="content">
            <div class="text">欢迎大家</div>
            <div class="info">
                <div class="critic">评论人:小红</div>
                <div class="time">评论时间: 2019-02-15</div>
            </div>
        </div>
        <div class="content">
            <div class="text">萨瓦迪卡</div>
            <div class="info">
                <div class="critic">评论人:小红</div>
                <div class="time">评论时间: 2019-02-15</div>
            </div>
        </div>
        <div class="content">
            <div class="text">宝塔镇河妖</div>
            <div class="info">
                <div class="critic">评论人:小红</div>
                <div class="time">评论时间: 2019-02-15</div>
            </div>
        </div>
        <div class="content">
            <div class="text">天王盖地虎</div>
            <div class="info">
                <div class="critic">评论人:小红</div>
                <div class="time">评论时间: 2019-02-15</div>
            </div>
        </div> -->
    </footer>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        const baseURL = 'http://www.liulongbin.top:3006';
        const searchURL = '/api/cmtlist';
        const addURL = '/api/addcmt';
        const footer = document.querySelector('footer');
        const inp = document.querySelector('input');
        const content = document.querySelector('textarea');
        const submitBtn = document.querySelector('.submit-btn');
        submitBtn.addEventListener('click', function () {
            axios({
                method: 'post',
                url: baseURL + addURL,
                data: {
                    username: inp.value,
                    content: content.value,
                },
            }).then((res) => {
                if (res.data.status === 501) {
                    alert(res.data.msg);
                } else {
                    search();

                }
            })
        })
        function search(params) {
            axios({
                method: 'get',
                url: baseURL + searchURL,
            }).then((res) => {
                footer.innerHTML = '';
                for (const item of res.data.data) {
                    const el = document.createElement('div');
                    el.setAttribute('class', 'content')
                    el.innerHTML = `
                            <div class="content">
                                <div class="text">评论内容:${item.content}</div>
                                <div class="info">
                                    <div class="critic">评论人: ${item.username}</div>
                                    <div class="time">评论时间: ${item.time}</div>
                                </div>
                            </div>
                    `;
                    footer.appendChild(el);
                }
            })
        }
        search();
    </script>
</body>
</html>